#coding:utf-8
import appconf
import datetime,uuid
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(appconf.app)

class Project(db.Model):
    __tablename__ = 'project'
    id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.String(50), nullable=True)
    last_time = db.Column(db.String(50), nullable=True)

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id
    def __init__(self,project_name):
        self.project_name = project_name
        self.last_time = str(datetime.datetime.now())

class MoudleApi(db.Model):
    __tablename__ = 'moudle'
    id = db.Column(db.Integer, primary_key=True)
    moudle_name = db.Column(db.String(50), nullable=True)
    moudle_type = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, nullable=True)
    last_time = db.Column(db.String(50), nullable=True)

    def add(self):
        db.session.add(self)
        db.session.commit()


    def __init__(self,moudle_name,project_id,moudle_type=0):
        self.moudle_name = moudle_name
        self.moudle_type = moudle_type
        self.project_id = project_id
        self.last_time = str(datetime.datetime.now())

class ApiPlan(db.Model):
    __tablename__ = 'api_plan'
    id = db.Column(db.Integer, primary_key=True)

    project_id = db.Column(db.Integer, nullable=True)
    module_id = db.Column(db.Integer, nullable=True)

    api_plan_name = db.Column(db.String(50), nullable=True)
    auto_order = db.Column(db.String(50), nullable=True)
    caseIdList = db.Column(db.Text, nullable=True)

    last_time = db.Column(db.String(50), nullable=True)
    isrun = db.Column(db.Integer, nullable=True)

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id

    def __init__(self, project_id,module_id,api_plan_name,auto_order,caseIdList='',isrun=0):
        self.project_id = project_id
        self.module_id = module_id
        self.api_plan_name = api_plan_name
        self.auto_order = auto_order
        self.caseIdList = caseIdList
        self.isrun = isrun
        self.last_time = str(datetime.datetime.now())

class ApiParam(db.Model):
    __tablename__ = 'api_param'
    id = db.Column(db.Integer, primary_key=True)

    project_id = db.Column(db.Integer, nullable=True)

    param_type = db.Column(db.Integer, nullable=True)

    param_name = db.Column(db.String(50), nullable=True)
    param_value = db.Column(db.String(500), nullable=True)

    case_id = db.Column(db.String(50), nullable=True)
    leftStr = db.Column(db.String(500), nullable=True)
    rightStr = db.Column(db.String(500), nullable=True)
    indexStr = db.Column(db.Integer, nullable=True)

    jsonObj = db.Column(db.String(200), nullable=True)

    last_time = db.Column(db.String(50), nullable=True)

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id
    def __init__(self,project_id,param_type,param_name,param_value,case_id='',leftStr='',rightStr='',
                 indexStr=0,jsonObj=''):
        self.project_id = project_id

        self.param_type = param_type
        self.param_name = "$%s$"%param_name
        self.param_value = param_value
        self.case_id = case_id
        self.leftStr = leftStr
        self.rightStr = rightStr
        self.indexStr = indexStr
        self.jsonObj = jsonObj
        self.last_time = str(datetime.datetime.now())



class ApiResult(db.Model):
    __tablename__ = 'api_result'
    id = db.Column(db.Integer, primary_key=True)

    project_id = db.Column(db.Integer, nullable=True)
    module_id = db.Column(db.Integer, nullable=True)
    api_plan_id = db.Column(db.Integer, nullable=True)
    path = db.Column(db.String(500), nullable=True)
    api_result = db.Column(db.String(500), nullable=True)
    case_id = db.Column(db.String(50), nullable=True)

    responseJson = db.Column(db.Text, nullable=True)
    responseJsonTime = db.Column(db.Text, nullable=True)

    last_time = db.Column(db.String(50), nullable=True)

    def add(self):
        db.session.add(self)
        db.session.commit()

    def __init__(self, project_id, module_id, api_plan_id, api_result,case_id,responseJson,responseJsonTime,path):
        self.project_id = project_id
        self.module_id = module_id
        self.api_plan_id = api_plan_id
        self.api_result = api_result
        self.case_id = case_id
        self.responseJson = responseJson
        self.responseJsonTime =responseJsonTime
        self.path = path
        self.last_time = str(datetime.datetime.now())



class ApiCase(db.Model):
    """存储结果表"""
    __tablename__ = 'api_case'
    id = db.Column(db.Integer, primary_key=True)

    case_id = db.Column(db.String(50), nullable=True)
    project_id = db.Column(db.Integer, nullable=True)
    module_id = db.Column(db.Integer, nullable=True)
    effect = db.Column(db.String(500), nullable=True)

    method = db.Column(db.String(10), nullable=True)
    req_type = db.Column(db.String(10), nullable=True)
    host = db.Column(db.String(100), nullable=True)
    path = db.Column(db.String(500), nullable=True)
    pre_caseId = db.Column(db.String(50), nullable=True)
    run_times = db.Column(db.Integer, nullable=True)
    concurrent = db.Column(db.Integer, nullable=True)

    note = db.Column(db.String(500), nullable=True)

    headersJson = db.Column(db.Text, nullable=True)
    bodysJson = db.Column(db.Text, nullable=True)
    chkJson = db.Column(db.Text, nullable=True)
    person = db.Column(db.String(50), nullable=True)

    is_show = db.Column(db.Integer, nullable=True)

    last_time = db.Column(db.String(50), nullable=True)

    def add(self):
        db.session.add(self)
        db.session.commit()
        return {"id":self.id,"caseId":self.case_id}

    def __init__(self,project_id,module_id,effect,method,req_type,host,path,pre_caseId,run_times,concurrent,
                 headersJson,bodysJson,chkJson,person):
        strUid = "%s"%uuid.uuid1()
        case_id = "%s%s"%(path[0:20],strUid[0:10])
        self.case_id = case_id
        self.last_time = str(datetime.datetime.now())

        self.project_id = project_id
        self.module_id= module_id
        self.effect= effect
        self.method= method
        self.req_type= req_type
        self.host= host
        self.path= path
        self.pre_caseId= pre_caseId
        self.run_times= run_times
        self.concurrent= concurrent
        self.headersJson= headersJson
        self.bodysJson= bodysJson
        self.chkJson= chkJson
        self.person= person
        self.is_show = 0


db.create_all()